﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Zip
{
    public class Extract1
    {
        private InflateZipFile Inflate;
        private List<FileHeader> ZipDir;

        public static int result;

        public Extract1()
        {

        }

        public int ExtractToDirectory(string fileName, string path)
        {
           Trace.Open("Zipfile.txt");
           ProgramState.LoadState();
           
            // create inflate object
            if (Inflate == null)
            {
                Inflate = new InflateZipFile();
            }

            // inflate is already open
            else if (InflateZipFile.IsOpen(Inflate))
            {
                // delete zip directory
                ZipDir = null;

                // close previous zip archive (if open)
                Inflate.CloseZipFile();

            }

            // open zip file and load zip file directory
            if (Inflate.OpenZipFile(fileName))
            {
                //   MessageBox.Show("Open ZIP file Error\n" + Inflate.ExceptionStack[0] + "\n" + Inflate.ExceptionStack[1]);
                return -1;
            }

            // get zip directory
            ZipDir = Inflate.ZipDir;
            ProgramState NewState = new ProgramState();

            NewState.ExtractToFolder = path;
            NewState.ExtractAll = true;
            NewState.Overwrite = (Int32)OverwriteFiles.Yes;
            NewState.IgnoreFolderInfo = false;
            NewState.SkipReadOnly = false;
            NewState.SkipHidden = false;
            NewState.SkipSystem = false;
            NewState.SkipOlder = false;

            // save program state
            ProgramState.SaveState(NewState);
            // create extract dialog
            ProcessFilesForm1 ExtractDialog = new ProcessFilesForm1();

            // user selected extract all
           
            if (ProgramState.State.ExtractAll)
            {
                ProcessFilesForm2.ZipDir = ZipDir;
            }
            // display extract dialog
            ProcessFilesForm2.UpdateMode = false;
            ProcessFilesForm2.Inflate = Inflate;
            ExtractDialog.OnLoad();
            ProcessFilesForm1.autoEvent1.WaitOne();

            Inflate.CloseZipFile();

            return result;
        }

        public int CompressToFile(string path1, string fileName )
        {

  //          string path = Directory.GetCurrentDirectory();

 //           path = string.Concat(path, "\\", fileName);
            Trace.Open("Zipfile.txt");

            string path = fileName;
            // create deflate object
            DeflateZipFile Deflate = new DeflateZipFile();

            // create empty zip file
            Deflate.CreateArchive(path);

            // get link to the empty zip directory
            ZipDir = Deflate.ZipDir;

            string[] fileEntries = Directory.GetFileSystemEntries(path1, "*.*");
            foreach (string InputFileName in fileEntries)
            {
                // split the input file name to path and name
                String FileName;
                Int32 Ptr = InputFileName.LastIndexOf('\\');
                FileName = Ptr < 0 ? InputFileName : InputFileName.Substring(Ptr + 1);

                //               MessageBox.Show(InputFileName, "   " + FileName);
                // compress file
                if (Deflate.Compress(InputFileName, FileName))
                {
                    // save exception stack on error
                    //                  ExceptionStack = Deflate.ExceptionStack;
                    return (0);
                }
            }

            Deflate.SaveArchive();
            return (0);
        }

        public int Compress1ToFile(string path1, string fileName )
        {

            return (0);
        }

    }
}
